<template>
    <div class="body">
        <img class="loginBg" src="@/assets/img/loginBg.png" alt="">
        <div class="login">
            <img class="logo" src="@/assets/img/logo.png" alt="">
            <div class="loginCont">
                <div class="leftBar" @click="vistor">游客登录</div>
                <div class="right">
                    <div class="input">
                        <van-icon :color="color.colorObj.color999" name="contact" />
                        <van-field v-model="userId" placeholder="请输入用户ID" />
                    </div>
                    <div class="input">
                        <van-icon :color="color.colorObj.color999" name="contact" />
                        <van-field v-model="password" type="password" placeholder="请输入密码" />
                    </div>
                    <ButtonC :btnDisabled='btnDisabled' style="margin-top:12px;" @btnEvent='loginBtn' :name="'登录'" :round="'round'" :color="`linear-gradient(to right, ${color.colorObj.color0B2D91}, ${color.colorObj.colorDA332D})`"/>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import color from '@/assets/style/color.js';
import { ButtonC } from '@/components'
// import commonFun from '@/utils/commonFun'
import { nanoid } from 'nanoid'  // 生成唯一id，uuid的简略版
export default({
    data() {
        return {
            userId: '',
            password: '',
            btnLoading: false,
            color:color
        }
    },
    components: {
        ButtonC
    },
    computed:{
        // 当用户id和密码不为空时，按钮可用
        btnDisabled(){
            if(this.userId && this.password){
                return false
            }else{
                return true
            }
        }
    },
    methods: {
        // 登录
        loginBtn(){
            this.btnLoading = true
            if(this.userId && this.password) {
                this.$store.dispatch('login',{user_id: this.userId,password: this.password}).then(res=>{
                    this.btnLoading = false
                    if(res){
                        // 输入成功则进入主页面
                        this.$router.push('Home')
                        localStorage.setItem('userMsg',JSON.stringify(res))
                    }else{
                        // 否则清空用户id和密码
                        this.userId = ''
                        this.password = ''
                    }
                })
            }
        },
        // 游客登录
        vistor(){
            let obj = {
                // id: 1,
                name: '游客',
                password: "123",
                user_id: nanoid(), //commonFun.randomString(7), // 随机生成
                user_type: "vistor"
            }
            localStorage.setItem('userMsg',JSON.stringify(obj))
            this.$router.push('Home')
        }
    }
})
</script>

<style lang="less" scoped>
@import '@/assets/style/color.less';
@import '@/assets/style/style.less';
.body{
    width: 100%;
    height: 84vh;
    .loginBg{
        width: 100%;
        height: 100vh;
        z-index: -100;
        position: absolute;
    }
    .login{
        background: @color_opacity_black;
        height: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;
        padding-top: 16vh;
        .logo{
            width: 220px;
            height: 46px;
            margin-bottom: 40px;
        }
        .loginCont{
            display: flex;
            flex-direction: row;
            padding: 0 30px;
            position: relative;
            background: @login-bg-opacity;
            border-radius: 6px;
            /deep/.van-cell{
                background-color: transparent;
                border-bottom: 1px solid @color_font_999;
            }
            .input{
                display: flex;
                flex-direction: row;
                align-items: center;
                margin-bottom: @padd_margin_12;
            }
            .leftBar{
                position: absolute;
                right: 16px;
                bottom: 2px;
                font-size: 12px;
                color: @color_font_666;
            }
            .right{
                display: flex;
                flex-direction: column;
                align-items: center;
                margin: 30px 0;
            }
        }
    }
    ::-webkit-input-placeholder {
    color: @color_font_666;font-size:12px;
    }

    /* Firefox 4-18 */
    :-moz-placeholder {
    color: @color_font_666;font-size:12px;
    }

    /* Firefox 19-50 */
    ::-moz-placeholder {
    color: @color_font_666;font-size:12px;
    }

    /* - Internet Explorer 10–11
    - Internet Explorer Mobile 10-11 */
    :-ms-input-placeholder {
    color: @color_font_666 !important;
    }

    /* Edge (also supports ::-webkit-input-placeholder) */
    ::-ms-input-placeholder {
    color: @color_font_666;font-size:12px;
    }

    /* CSS Working Draft */
    ::placeholder {
    color: @color_font_666;font-size:12px;
    }

}
</style>